package com.hit.aircraftwar.application;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hit.aircraftwar.R;
import com.hit.aircraftwar.dao.Record;

import java.util.LinkedList;


public class RankAdapter extends BaseAdapter {
    private final Context context;
    private final LinkedList<Record> recordList;
    private int selectedPosition = -1;

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
        notifyDataSetChanged();
    }

    public RankAdapter(Context context, LinkedList<Record> recordList) {
        this.context = context;
        this.recordList = recordList;
    }
    //获取填充的item个数
    @Override
    public int getCount() {
        return recordList.size();
    }
    //指定索引对应的item的数据项
    @Override
    public Record getItem(int position) {
        return recordList.get(position);
    }
    //指定索引对应的item的id值
    @Override
    public long getItemId(int position) {
        return position;
    }
    //填充每个item的内容
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null){
            view= LayoutInflater.from(context).inflate(R.layout.rank_line,null);
            holder = new ViewHolder();
            holder.rank = view.findViewById(R.id.Ranking);
            holder.name = view.findViewById(R.id.UserName);
            holder.score1= view.findViewById(R.id.Score);
            holder.time = view.findViewById(R.id.Time);
            view.setTag(holder);
        }else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        if(recordList.get(position) != null) {
            holder.rank.setText(String.valueOf(position+1));
            holder.name.setText(recordList.get(position).getId());
            holder.score1.setText(String.valueOf(recordList.get(position).getScore()));
            holder.time.setText(recordList.get(position).getTimeInfo());
        }else {
            holder.rank.setText(null);
            holder.name.setText(null);
            holder.score1.setText(null);
            holder.time.setText(null);
        }
        return view;
    }

    public static class ViewHolder{
        public  TextView rank;
        public  TextView name;
        public  TextView score1;
        public  TextView time;
    }
}
